package com.cuit.controller;

import com.cuit.dao.impl.OrderDaoImpl;
import com.cuit.pojo.Order;
import com.cuit.pojo.Patient;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/deleteOneOrder")
public class DeleteOneOrder extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession se = req.getSession();
        Patient patient = (Patient)se.getAttribute("patient");
        PrintWriter out = resp.getWriter();
        BufferedReader read = new BufferedReader(req.getReader());
        String number=null;
        String str=null;
        while((str=read.readLine())!=null){
            number=str;
        }
        System.out.println("编号："+number);
        String name="orderInfo"+number.charAt(1);
        System.out.println(name);
        Order order= (Order) se.getAttribute(name);
        String p_id = patient.getP_id();
        OrderDaoImpl dao = new OrderDaoImpl();
        System.out.println("编号："+number+",id："+order.getP_id());
        int i = dao.deleteFromOrder1(order.getP_id(), order.getD_id(), order.getTime(), order.getDay());
        int i1 = dao.updateFromOrder2(order.getD_id(), order.getDay());
        if(i==1&&i1>0) {
            System.out.println("删除成功");
            out.println("success");
        }else{
            System.out.println("删除失败");

        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
